package com.czzn.czkongzhi;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.csym.bluetoothlib.helper.IBluzScanHelper;
import com.csym.bluetoothlib.manager.MeasureDataListener;
import com.csym.bluetoothlib.utils.Beuse;


/**
 * Created by Administrator on 2017/11/15 0015.
 */

public class FunctionActivity extends Activity{

    //基本成员
    private TextView name,blt_status;
    private FrameLayout mContentLayout;
    Beuse beuse = new Beuse();

    private MeasureDataListener myMeasure;

    private final int MSG_START_DISCOVERY = 100;

    private MeasureDataListener mDataListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.function_main);
        //con_btl.setOnClickListener(this);
        mContentLayout=findViewById(R.id.layout_content);
        name=findViewById(R.id.name);
        blt_status=findViewById(R.id.blt_status);
        //setupViews();
    }

    private void setupViews(){

        beuse.init(FunctionActivity.this,new IBluzScanHelper.OnConnectionListener(){

            @Override
            public void onConnected(BluetoothDevice device) {
                blt_status.setText("已连接"+device.getName());
                String a="E8A101";
                byte[] send=stringToBytes(a);
                beuse.send(send);
                Toast.makeText(getApplication(),"已连接",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDisconnected(BluetoothDevice device) {
                blt_status.setText("蓝牙未连接");
                Toast.makeText(getApplication(),"失去连接",Toast.LENGTH_SHORT).show();
            }
        } );
    }

    //设置标题内容
    @Override
    public void setTitle(int titleId) {
        name.setText(titleId);
    }

    //设置标题内容
    @Override
    public void setTitle(CharSequence title) {
        name.setText(title);
    }


    public void setTitleColor(int textColor) {
        name.setTextColor(textColor);
    }

    //取出FrameLayout并调用父类removeAllViews()方法
    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }

    public void conBlt(View v){
        setupViews();
    }

    public void goBack(View v){
        finish();
    }


//    @Override
//    protected void onDestroy() {
//        beuse.release(FunctionActivity.this);
//        super.onDestroy();
//    }
//
//    @Override
//    protected void onStop() {
//        beuse.onStop();
//        super.onStop();
//    }
//
//    @Override
//    protected void onResume() {
//        beuse.onStart();
//        super.onResume();
//    }

    private byte[] stringToBytes(String s) {
        byte[] buf = new byte[s.length() / 2];
        for (int i = 0; i < buf.length; i++) {
            try {
                buf[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return buf;
    }
}
